package edu.gatech.arktos.testcases;

import java.io.File;
import java.util.Set;

import junit.framework.TestCase;
import edu.gatech.arktos.Assignment;
import edu.gatech.arktos.Constants;
import edu.gatech.arktos.GradesDB;
import edu.gatech.arktos.Project;
import edu.gatech.arktos.Session;
import edu.gatech.arktos.Student;
import edu.gatech.arktos.Team;

public class GradesDBTest2 extends TestCase {
    private Session session = null;

    GradesDB db = null;

    @Override    
    protected void setUp() throws Exception {
        session = new Session();
        session.login(Constants.USERNAME, Constants.PASSWORD);
        db = session.getDBByName(Constants.GRADES_DB); 
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        session.logout();
        super.tearDown();
    }

    // ============================================================

    // Assignment Tests
    
    public void testGetAssignmentByNumber() {
        Assignment assignment = null;
        try {
            assignment = db.getAssignmentByNumber(1);
        } catch (Exception e) {
            fail("Exception");
        }
        assertNotNull(assignment);
        assertTrue(assignment.getDescription().compareTo("swiki page") == 0);
    }

    public void testGetNumAssignments() {
        int numassignments = db.getNumAssignments();
        assertEquals(3, numassignments);
    }

    public void testAssignmentScore() {
        try {
            Assignment a = db.getAssignmentByNumber(3);
            Student s = db.getStudentByName("Shevon Wise");
            assertEquals(40, s.getGradeForAssignment(a));
        } catch (Exception e) {
            fail("Exception while getting assignment number 3");
        }
    }

    // Project Tests
    
    public void testGetProjectsByName() {
        Project project = null;
        try {
            project = db.getProjectByName("P1");
        } catch (Exception e) {
            fail("Exception");
        }
        assertNotNull(project);
        assertTrue(project.getProjectDescription().compareTo(
                "WordCount in Java") == 0);
    }

    public void testGetNumProjects() {
        int numprojects = db.getNumProjects();
        assertEquals(3, numprojects);
    }

    public void testProjectAverageScore() {
        try {
            Project p = db.getProjectByName("P1");
            assertEquals(93, p.getAverageScore());
        } catch (Exception e) {
            fail("Exception");
        }
    }
    
    public void testProjectTeams() {
        Project p = null;
        try {
            p = db.getProjectByName("P3");
        } catch (Exception e) {
            fail("Exception");
        }
        Set<Team> t = p.getTeams();
        assertEquals(3, t.size());
    }

    // Team Tests

    public void testGetMembers() {
        try {
            Team team = db.getProject("P1").getTeamByName("Team 1");
            Set<Student> members = team.getMembers();
            
            assertTrue(members.contains(db.getStudentByName("Freddie Catlay")));
            assertTrue(members.contains(db.getStudentByName("Shevon Wise")));
            assertTrue(members.contains(db.getStudentByName("Kym Hiles")));
            assertTrue(members
                    .contains(db.getStudentByName("Ernesta Anderson")));
            assertTrue(members.contains(db.getStudentByName("Sheree Gadow")));
        } catch (Exception e) {
            fail("Exception");
        }
    }

    public void testGetTeamScore() {
        try {
            Team team = db.getProject("P1").getTeamByName("Team 1");
            
           
            assertEquals("93", team.getTeamScore());
        } catch (Exception e) {
        	e.printStackTrace();
            fail("Exception");
        }
    }


    public void testAttendance() {
        try {
            Student student = db.getStudentByName("Grier Nehling");
            assertEquals(96, student.getAttendance());
        } catch (Exception e) {
            fail("Exception");
        }
    }

    public void testName() {
        try {
            Student student = db.getStudentByID("901234503");
            assertEquals("Grier Nehling", student.getName());
        } catch (Exception e) {
            fail("Exception");
        }
    }

    
   
     
    public void testGtid() {
        try {
            Student student = db.getStudentByName("Grier Nehling");
            assertEquals("901234503", student.getGtid());
        } catch (Exception e) {
            fail("Exception");
        }
    }

    public void testEmail() {
        try {
            Student student = db.getStudentByName("Grier Nehling");
            assertEquals("gn@gatech.edu", student.getEmail());
        } catch (Exception e) {
            fail("Exception");
        }
    }

    public void testCheckAll() {
        try {
            Student student = db.getStudentByName("Grier Nehling");
            assertEquals("901234503", student.getGtid());
            assertEquals(96, student.getAttendance());
            assertEquals("Grier Nehling", student.getName());
            assertEquals("gn@gatech.edu", student.getEmail());
        } catch (Exception e) {
            fail("Exception");
        }
    }

    // FileSave Tests

    public void testFileSave() {
        try {
            Student student = db.getStudentByID("901234510");
           
            student.saveInfoToFile();
            File file = new File("ErnestaAnderson.txt");
            assertTrue(file.exists());
        } catch (Exception e) {
        	e.printStackTrace();
            fail("Exception");
        }
    }

    // ============================================================

}
